package com.rabbit.client;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class client01  {
    private  static  final  String QUEQUE="helloworld";

    public  static void main(String[] args){
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        Connection connection=null;
        try {
            connection=connectionFactory.newConnection();
            Channel channel= connection.createChannel();

            //声明队列
            channel.queueDeclare(QUEQUE,true,false,false,null);

            DefaultConsumer defaultConsumer =new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String Exchange = envelope.getExchange();
                    String message = new String(body,"UTF-8");
                    System.out.println(message);
                   /* Scanner scanner=new Scanner(System.in);
                    String s=scanner.next();
                    System.out.println(s);*/
                }
            };

            //监听队列
            channel.basicConsume(QUEQUE,true,defaultConsumer);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
